package com.john.utils.common;

import java.io.*;

/**
 * 文件操作工具类
 *
 */
public class FileUtils {
	/**
	 * 获取文件扩展名
	 *
	 * @param filename
	 * @return
	 */
	public static String getExtend(String filename) {
		return getExtend(filename, "");
	}

	/**
	 * 获取文件扩展名
	 *
	 * @param filename
	 * @return
	 */
	public static String getExtend(String filename, String defExt) {
		if ((filename != null) && (filename.length() > 0)) {
			int i = filename.lastIndexOf('.');

			if ((i > 0) && (i < (filename.length() - 1))) {
				return (filename.substring(i+1)).toLowerCase();
			}
		}
		return defExt.toLowerCase();
	}

	/**
	 * 获取文件名称[不含后缀名]
	 *
	 * @param
	 * @return String
	 */
	public static String getFilePrefix(String fileName) {
		int splitIndex = fileName.lastIndexOf(".");
		return fileName.substring(0, splitIndex).replaceAll("\\s*", "");
	}

	/**
	 * 获取文件名称[不含后缀名]
	 * 不去掉文件目录的空格
	 * @param
	 * @return String
	 */
	public static String getFilePrefix2(String fileName) {
		int splitIndex = fileName.lastIndexOf(".");
		return fileName.substring(0, splitIndex);
	}

	/**
	 * 文件复制
	 *方法摘要：这里一句话描述方法的用途
	 *@param
	 *@return void
	 */
	public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{
		File sFile = new File(inputFile);
		File tFile = new File(outputFile);
		FileInputStream fis = new FileInputStream(sFile);
		FileOutputStream fos = new FileOutputStream(tFile);
		int temp = 0;
		byte[] buf = new byte[10240];
        try {
        	while((temp = fis.read(buf))!=-1){
        		fos.write(buf, 0, temp);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try {
            	fis.close();
            	fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }
	}

	/**
	 * 删除指定的文件
	 *
	 * @param strFileName
	 *            指定绝对路径的文件名
	 * @return 如果删除成功true否则false
	 */
	public static boolean delete(String strFileName) {
		File fileDelete = new File(strFileName);

		if (!fileDelete.exists() || !fileDelete.isFile()) {
			System.out.println("错误: " + strFileName + "不存在!");
			return false;
		}

		System.out.println("--------成功删除文件---------"+strFileName);
		return fileDelete.delete();
	}
	/**
	 * 根据链接获取文件名称包含后缀
	 * @param filePath
	 * @return
	 */
	public static String getFileNameExtend(String filePath){
		System.out.println(filePath);
		return filePath.substring(filePath.lastIndexOf("/")+1,filePath.length());
	}

	/**
	 * File转byte[]数组
	 *
	 * @param fileFullPath
	 * @return
	 */
	public static byte[] file2byte(String fileFullPath) {
		if (fileFullPath == null || "".equals(fileFullPath)) {
			return null;
		}
		return file2byte(new File(fileFullPath));
	}

	/**
	 * File转byte[]数组
	 *
	 * @param file
	 * @return
	 */
	public static byte[] file2byte(File file) {
		if (file == null) {
			return null;
		}
		FileInputStream fileInputStream = null;
		ByteArrayOutputStream byteArrayOutputStream = null;
		try {
			fileInputStream = new FileInputStream(file);
			byteArrayOutputStream = new ByteArrayOutputStream();
			byte[] b = new byte[1024];
			int n;
			while ((n = fileInputStream.read(b)) != -1) {
				byteArrayOutputStream.write(b, 0 , n);
			}
			return byteArrayOutputStream.toByteArray();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileInputStream != null) {
					fileInputStream.close();
				}
				if (byteArrayOutputStream != null) {
					byteArrayOutputStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * byte[]数组转File
	 *
	 * @param bytes
	 * @param fileFullPath
	 * @return
	 */
	public static File byte2file(byte[] bytes, String fileFullPath) {
		if (bytes == null) {
			return null;
		}
		FileOutputStream fileOutputStream = null;
		try {
			File file = new File(fileFullPath);
			//判断文件是否存在
			if (file.exists()) {
				file.mkdirs();
			}
			fileOutputStream = new FileOutputStream(file);
			fileOutputStream.write(bytes);
			return file;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			}  catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * byte[]数组转File
	 *
	 * @param bytes
	 * @return
	 */
	public static File byte2file(byte[] bytes, File file) {
		if (bytes == null) {
			return null;
		}
		FileOutputStream fileOutputStream = null;
		try {
			//判断文件是否存在
			if (file.exists()) {
				file.mkdirs();
			}
			fileOutputStream = new FileOutputStream(file);
			fileOutputStream.write(bytes);
			return file;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (fileOutputStream != null) {
					fileOutputStream.close();
				}
			}  catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
		return null;
	}
}
